package cn.gson.springboot.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.gson.springboot.model.pojos.DestructionYk;
import cn.gson.springboot.model.pojos.ReturnDrugsYk;
import cn.gson.springboot.model.service.ReturnDrugsYkService;
import cn.gson.springboot.model.service.ReturnDrugsYkxqService;
import cn.gson.springboot.model.vo.DestructionYkVO;
import cn.gson.springboot.model.vo.ReturnDrugsYkVO;

@Controller
public class ReturnDrugsYkController {
	@Autowired
	private ReturnDrugsYkService rds;
	@Autowired
	private ReturnDrugsYkxqService rdxqs;
	
	
	//查询所有
	@RequestMapping("thbg")
	@ResponseBody
	public Map<String,Object> hosLikeTableSmt(
		@RequestParam("page")int page,
		@RequestParam("rows")int rows,Integer return_drugs_id){			
				
		//进行分页
		Page<Object> pg = PageHelper.startPage(page, rows);
		JSONArray findall=rds.findtuihuo(return_drugs_id);
				
		//进行数据格式转换和封装
		Map<String,Object> map = new HashMap<>();
		map.put("total", pg.getTotal());//封装总记录数
		map.put("rows", findall);//封装当前页的内容
				
		return map; 
	}
	
	//退货主从表新增
	@RequestMapping("tuihuo-save")
	@ResponseBody
	public String saveOrder(ReturnDrugsYkVO purVo) {
		try {
			//System.out.println(purVo.getTuihuo().getReturnDrugsNumber());
			rds.saveOrder(purVo);
			return "新增成功";
		} catch (Exception e) {
			e.printStackTrace();
			return "新增失败";
		}
	} 
	
	//退货详情查询所有
	@RequestMapping("tuihxq")
	@ResponseBody
	public JSONArray findbythxqid(Integer return_drugs_id) {
		 JSONArray findbyidthxq = rdxqs.findbyidthxq(return_drugs_id);
		 System.out.println(findbyidthxq.toString());
		 return findbyidthxq;
	}
	
	//修改状态
	@PostMapping("xgztid")
	@ResponseBody
	public String idxx(Integer returnDrugsId) {
		ReturnDrugsYk l = rds.idxgzt(returnDrugsId);
		if(l!=null) {
			l.setReturnStatic("已退货");
			rds.adds(l);
			return "1";
		}else {
			return "0";
		}
	}
}
 